#include "block.h"
#include <iostream>
//class Quad;

Block::Block(int *loc)
{
    //std::cout<<"Initializing block\n";
    Quad myFace;
    myFace.empty();
    //build faces
    int x, y, z, face;
    if (loc[0] == 1)
    {
        myFace.empty();
        
        x = 3 * hBlk;
        y = loc[1];
        z = loc[2];
        face = coorToIndex(y, z);   //map y, z to face index
        
        for(int i = 0; i < 4; i++)
        {

            myFace.addVector(x, vertex2f[ vertex2fIndex[face][i] ][0],
                   vertex2f[ vertex2fIndex[face][i] ][1]);
        }
        myFace.addVector(1.0f, 0.0f, 0.0f);
        myFace.addVector(ORANGE);    //orange
        faces.push_back(myFace);
        //debug 
    }

    else if (loc[0] == -1)
    {

        myFace.empty();
        x = -3 * hBlk;
        y = loc[1];
        z = loc[2];
        face = coorToIndex(y, z);   //map y, z to face index
        
        for(int i = 3; i >= 0; i--)
            myFace.addVector(x, vertex2f[ vertex2fIndex[face][i] ][0],
                    vertex2f[ vertex2fIndex[face][i] ][1]);
        myFace.addVector(-1.0f, 0.0f, 0.0f);
        myFace.addVector(RED);    //red
        faces.push_back(myFace);

    }

    if (loc[1] == 1)
    {
        myFace.empty();
        x = loc[0];
        y = 3 * hBlk;
        z = loc[2];
        
        face = coorToIndex(x, z);   //map x, z to face index
        for(int i = 3; i >= 0; i--)
            myFace.addVector(vertex2f[ vertex2fIndex[face][i] ][0], y,
                    vertex2f[ vertex2fIndex[face][i] ][1]);
        myFace.addVector( 0.0f, 1.0f, 0.0f);
        myFace.addVector(BLUE);    //orange
        faces.push_back(myFace);
    }

    else if (loc[1] == -1)
    {
        myFace.empty();
        x = loc[0];
        y = -3 * hBlk;
        z = loc[2];
        
        face = coorToIndex(x, z);   //map x, z to face index
        for(int i = 0; i < 4; i++)
            myFace.addVector(vertex2f[ vertex2fIndex[face][i] ][0], y,
                vertex2f[ vertex2fIndex[face][i] ][1]);
        myFace.addVector( 0.0f, 1.0f, 0.0f);
        myFace.addVector(GREEN);    //orange
        faces.push_back(myFace);
    }

    if (loc[2] == 1)
    {
        myFace.empty();
        
        x = loc[0];
        y = loc[1];
        z = 3 * hBlk;
        face = coorToIndex(x, y);   //map x, y to face index
        for(int i = 0; i < 4; i++)
            myFace.addVector(vertex2f[ vertex2fIndex[face][i] ][0],
                        vertex2f[ vertex2fIndex[face][i] ][1], z);
        myFace.addVector( 0.0f, 0.0f, 1.0f);
        myFace.addVector(YELLOW);    //YELLOW
        faces.push_back(myFace);
    }

    else if (loc[2] == -1)
    {
        myFace.empty();
        x = loc[0];
        y = loc[1];
        z = -3 * hBlk;
        face = coorToIndex(x, y);   //map x, y to face index
        for(int i = 3; i >= 0; i--)
            myFace.addVector(vertex2f[ vertex2fIndex[face][i] ][0],
                vertex2f[ vertex2fIndex[face][i] ][1], z);
        myFace.addVector( 0.0f, 0.0f, -1.0f);
        myFace.addVector(WHITE);    //YELLOW
        faces.push_back(myFace);
    }
    //debug
    //std::cout<<loc[0]<<loc[1]<<loc[2]<<" "<<myFace.size()<<std::endl;
}

void Block::glDraw()
{
    for( int i = 0; i < faces.size(); i++)
    {
        faces[i].glDraw();
    }
}
        
void Block::rotate(char direction, GLfloat ang)
{
    //clock wise rotate
    //just rotate in a single moment
    switch(direction)
    {
        case 'z': //rotate alone z
            for( int i = 0; i < faces.size() ; i++)
                faces[i].rotateZ(ang);
            break;
        case 'x': //rotate alne x
            for( int i = 0; i < faces.size() ; i++)
                faces[i].rotateX(ang);
            break;
        case 'y': //rotate alne y
            for( int i = 0; i < faces.size() ; i++)
                faces[i].rotateY(ang);
            break;
        default: break;
    }
}

        
